package pod

import (
	"context"
	"fmt"
	"go-pod/api/internal/svc"
	"go-pod/api/internal/types"
	"go-pod/pod-rpc/pod"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListPodcastsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewListPodcastsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListPodcastsLogic {
	return &ListPodcastsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ListPodcastsLogic) ListPodcasts() (resp *types.PodListResp, err error) {
	// 设置默认分页参数
	page := int64(1)
	pageSize := int64(10)
	keyword := ""

	// 调用RPC服务获取播客列表
	rpcResp, err := l.svcCtx.PodRpcClient.ListPodcasts(l.ctx, &pod.ListPodcastsReq{
		Page:     page,
		PageSize: pageSize,
		Keyword:  keyword,
	})
	if err != nil {
		l.Logger.Errorf("获取播客列表失败: %v", err)
		return nil, fmt.Errorf("获取播客列表失败，请稍后重试")
	}

	// 构建响应
	podcastList := make([]types.PodcastResp, 0, len(rpcResp.List))
	for _, item := range rpcResp.List {
		podcastList = append(podcastList, types.PodcastResp{
			Id:         item.Id,
			Title:      item.Title,
			Content:    item.Content,
			Created_at: item.CreatedAt,
			Updated_at: item.UpdatedAt,
		})
	}

	// 记录成功日志
	l.Logger.Infof("获取播客列表成功，总数: %d", rpcResp.Total)

	return &types.PodListResp{
		List: podcastList,
	}, nil
}
